package com.example.wwj.controller;


import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.io.IoUtil;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.example.wwj.pojo.*;
import com.example.wwj.service.StudentService;
import com.example.wwj.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.*;

@RestController
@RequestMapping("/student")
public class studentController {

    @Autowired
    private StudentService studentService;


    /*
    * 查询所有的学生
    * */
    @GetMapping("/studentList")
    public List<Student> getStudentList() {
        return studentService.findAll();
    }


    /*
    * 查询所有的班级
    * */

    @GetMapping("/classList")
    public List<ClassName> getClassNameList() {
        return studentService.classNameList();
    }

    /*
    * 查询所有的年级
    * */

    @GetMapping("/gradeList")
    public List<GradeName> getGradeNameList() {
        return studentService.gradeNameList();
    }

    /*
    * 根据班级id查询班级名称
    * class_id:班级id
    * */
    @GetMapping("/findClassNameById/{class_id}")
    public ClassName getClassName(@PathVariable Integer class_id) {
        return studentService.findClassNameById(class_id);
    }

    /*
    * 根据年级id查询年级名称
    * grade_id:年级id
    * */
    @GetMapping("/findGradeNameById/{grade_id}")
    public GradeName getGradeName(@PathVariable Integer grade_id) {
        return studentService.findGradeNameById(grade_id);
    }

    /*
    * 新增保存
    * */
    
    @PostMapping("/save")
    public ResultVo save(@RequestBody Student student) {
        return studentService.save(student);
    }

    @PostMapping("/update")
    public ResultVo update(@RequestBody Student student){
        return studentService.update(student);
    }
    /*
    * 学生信息分页查询
    * pageNum:页码
    * pageSize:一页几个数据
    * total：数据总数
    * classId:班级id
    * gradeId:年级id
    * name:学生姓名
    * */
    @GetMapping("/page")
    public Map<String, Object> findPage(
            @RequestParam(required = false) Integer pageNum,
            @RequestParam(required = false) Integer pageSize,
            @RequestParam(required = false) Integer class_id,
            @RequestParam(required = false) Integer grade_id,
            @RequestParam(required = false) String name
    ) {
        if (pageNum == null || pageSize == null) {
            // 处理缺少参数的情况，这里可以返回错误信息或者设置默认值
        }

        pageNum = (pageNum - 1) * pageSize;

        List<Student> data;
        Integer total;

        if (class_id != null || grade_id != null || (name != null && !name.isEmpty())) {
            data = studentService.selectPage(pageNum, pageSize, class_id, grade_id, name);
            total = studentService.selectTotal(class_id, grade_id, name);
        } else {
            data = studentService.selectPage(pageNum, pageSize, null, null, null); // 查询所有数据
            total = studentService.selectTotal(class_id, grade_id, name);
        }

        Map<String, Object> res = new HashMap<>();
        res.put("data", data);
        res.put("total", total);
        return res;
    }
    /*
    * 批量删除
    * ids:学生的id的列表
    * */
    @GetMapping("/delete")
    public int delete(@RequestParam("ids") List<Integer> ids) {
        return studentService.deleteById(ids);
    }

    /*
    * 导出
    * */
    @GetMapping("/export")
       public ResultVo export(HttpServletResponse response) throws IOException {
        //查到所有数据
        List<Student> all = studentService.findAll();

        if(CollectionUtil.isEmpty(all)){
           ResultVo.errorVo(404,"没有找到数据");
        }

        List<Map<String,Object>> list = new ArrayList<>(all.size());

        for(Student student : all){
            Map<String,Object> row = new HashMap<>();
           row.put("姓名",student.getName());
           row.put("性别",student.getSex());
           row.put("年龄",student.getAge());
           row.put("班级",student.getClass_name());
           row.put("年级",student.getGrade_name());
           row.put("身份证号",student.getId_num());
           row.put("入学日期",student.getEnrollment_date());
           row.put("家庭地址",student.getAddress());
           row.put("监护人姓名",student.getGuardian_name());
           row.put("监护人联系方式",student.getGuardian_phone());
            list.add(row);
        }

        ExcelWriter wr = ExcelUtil.getWriter(true);
        wr.write(list,true);

        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
        response.setHeader("Content-Disposition","attachment;filename=student.xlsx");
        ServletOutputStream out = response.getOutputStream();
        wr.flush(out,true);
        wr.close();
        IoUtil.close(System.out);

            return  ResultVo.successVo();
    }
}
